package teleradiologia.service;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import teleradiologia.domain.Customer;
import teleradiologia.domain.Diagnosticlab;
import teleradiologia.domain.Ordering;
import teleradiologia.domain.Ordertype;
import teleradiologia.domain.Patient;
import teleradiologia.domain.Technician;
import teleradiologia.repository.OrderingDao;
import teleradiologia.web.TechnicianController;

public class OrderingManipulatorImpl implements OrderingManipulator {
	private OrderingDao orderingDao;

	private static final Log log = LogFactory
			.getLog(TechnicianController.class);

	@Override
	public long create(Ordering ordering, Patient patient, Customer customer,
			Ordertype ordertype, Diagnosticlab diagnosticlab,
			Technician technician) {
		ordering.setOrdertype(ordertype);
		ordering.setCustomer(customer);
		ordering.setPatient(patient);
		ordering.setDiagnosticlab(diagnosticlab);
		ordering.setTechnician(technician);
		return this.orderingDao.create(ordering);
	}

	@Override
	public void delete(Ordering ordering) {
		this.orderingDao.delete(ordering);

	}

	@Override
	public Ordering getById(long id) {
		return this.orderingDao.getById(id);
	}

	@Override
	public void setOrderingDao(OrderingDao orderingDao) {
		this.orderingDao = orderingDao;
	}

	@Override
	public void update(Ordering ordering) {
		this.orderingDao.update(ordering);
	}

	@Override
	public List<Ordering> getAllByTechnician(String nip) {
		return this.orderingDao.getAllByTechnician(nip);
	}

	@Override
	public List<Ordering> getAllByDiagnosticlab(String krs) {
		return this.orderingDao.getAllByDiagnosticlab(krs);
	}

	@Override
	public List<Ordering> getAllByPatient(long idpatient) {
		return this.orderingDao.getAllByPatient(idpatient);
	}

	@Override
	public void updateToTechnicianAchive(String nip) {
		List<Ordering> orderings = this.orderingDao
				.getAllByTechnicianToArchive(nip);

		for (Ordering o : orderings) {
			o.setStatus("archiwalne");
			this.orderingDao.update(o);
		}
	}
	
	@Override
	public void updateToDiagnosticlabAchive(String krs) {
		List<Ordering> orderings = this.orderingDao
				.getAllByDiagnosticlabToArchive(krs);
		
		log.info("orderings size: " + orderings.size());
		
		for (Ordering o : orderings) {
			o.setStatus("archiwalne");
			this.orderingDao.update(o);
		}
	}
}
